// @ts-nocheck
import store from '@/store/index'
import { dictGetDictList } from '@/api/public'

/**
 * @type {import('@/types/global').useDict}
 */
export const useDict = async (keys, retrieve = true) => {
  let userDict = store.state.user.userDict

  if (retrieve) {
    const { data } = await dictGetDictList(keys)
    userDict = data
  }
  let obj = {};
  userDict.map((item) => {
    item.label = item.dictLabel;
    item.value = item.dictValue;
    if (obj[item.dictType]) {
      obj[item.dictType].push(item)
    } else {
      obj[item.dictType] = [item]
    }
  });

  let res = {}
  // 多个字典 如 ['purchase_type','deliver_goods_cn']
  if (Array.isArray(keys)) {
    keys.map((key) => {
      res[key] = obj[key]
    })
  } else {
    res[keys] = obj[keys]
  }
  if (process.env.NODE_ENV == 'development') {
    const printKeys = ['dictType', 'dictName', 'dictLabel', 'dictValue',]
    Object.keys(res).forEach(key => {
      // console.table(res[key], printKeys)
    })
  }
  return res

};
